package nancal.mp

import nbcp.base.extend.*
import nbcp.base.json.*
import nbcp.base.utils.SpringUtil
import nbcp.base.utils.StringUtil
import org.slf4j.LoggerFactory
import org.springframework.amqp.rabbit.annotation.EnableRabbit
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
import org.springframework.boot.web.context.WebServerApplicationContext
import org.springframework.boot.web.servlet.ServletComponentScan
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.openfeign.EnableFeignClients
import org.springframework.context.ApplicationContext
import org.springframework.scheduling.annotation.Async
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor
import java.util.*


@SpringBootApplication(
    exclude = arrayOf(
        DataSourceAutoConfiguration::class,
        ElasticsearchDataAutoConfiguration::class
    )
)
@ServletComponentScan
@EnableDiscoveryClient
@EnableFeignClients(value = arrayOf("nbcp", "nancal"))

@Async
@EnableRabbit
open class IntegrationMainApplication {
    companion object {
        val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
    }
}


fun main(args: Array<String>) {
//    局显示定义最省事安全
//    Locale.setDefault(Locale.ENGLISH);//推荐用英语地区的算法
//    System.setProperty("user.timezone", "Asia/Shanghai");
//    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));

    //JDK8,11,14 需要添加 tls
    System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
    var context = SpringApplication.run(IntegrationMainApplication::class.java, *args)



    IntegrationMainApplication.logger.Important(
        StringUtil.getCenterEachLine(
            """
================================================
${context.debugServerInfo2}
================================================
""".split("\n")
        )
            .map { ' '.NewString(6) + it }
            .joinToString("\n")
    )
}


private val ApplicationContext.debugServerInfo2: String
    get() {
        var list = mutableListOf<String>()
        if (this is WebServerApplicationContext) {
            var port = this.environment.getProperty("server.port")
            list.add("${this.webServer.javaClass.simpleName}:${port}")
        }
        var applicationName = this.environment.getProperty("spring.application.name")
        var version = this.environment.activeProfiles.joinToString(",")

        list.add("${applicationName}:${version}")

        if (SpringUtil.containsBean<ScheduledAnnotationBeanPostProcessor>()) {
            list.add("@EnableScheduling")
        }

        return list.joinToString(" -- ");
    }


